package com.lmx.videoplay;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;

import com.lmx.videoplay.image.ImageDetailsActivity;
import com.lmx.videoplay.video.VideoDetailsActivity;
import com.qw.soul.permission.SoulPermission;
import com.qw.soul.permission.bean.Permission;
import com.qw.soul.permission.bean.Permissions;
import com.qw.soul.permission.callbcak.CheckRequestPermissionsListener;
import com.qw.soul.permission.callbcak.GoAppDetailCallBack;

/**
 * Author : Z-JC
 * Date : 2020/3/6
 * Description :
 */
public class MainActivity extends Activity implements View.OnClickListener {

    private Button btnVideo;
    private Button btnImage;
    public String[] permission = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= 23) {
//            Log.e(TAG, "申请权限");
            SoulPermission.getInstance().checkAndRequestPermissions(
                    Permissions.build(permission), new CheckRequestPermissionsListener() {
                        @Override
                        public void onAllPermissionOk(Permission[] allPermissions) {
                            // TODO: 2019/11/21 展示广告
                        }

                        @Override
                        public void onPermissionDenied(Permission[] refusedPermissions) {
                            StringBuilder sb = new StringBuilder();
                            for (Permission permission : refusedPermissions) {
                                sb.append(permission.getPermissionNameDesc()).append("\n");
                            }
                            String desc = sb.toString();
                            new AlertDialog.Builder(MainActivity.this)
                                    .setTitle("提示")
                                    .setCancelable(false)
                                    .setMessage("权限异常，请前往设置－>权限管理，打开" + desc)
                                    .setPositiveButton("去设置", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                            //去设置页
                                            SoulPermission.getInstance().goApplicationSettings(new GoAppDetailCallBack() {
                                                @Override
                                                public void onBackFromAppDetail(Intent data) {

                                                }
                                            });
                                        }
                                    }).create().show();
                        }
                    });
        }
        initView();
    }

    private void initView() {
        btnVideo = (Button) findViewById(R.id.btn_video);
        btnImage = (Button) findViewById(R.id.btn_image);

        btnVideo.setOnClickListener(this);
        btnImage.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_video:
                startActivity(new Intent(this, VideoDetailsActivity.class));
                break;
            case R.id.btn_image:
                startActivity(new Intent(this, ImageDetailsActivity.class));
                break;
        }
    }
}
